/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufpr.inf.escel.manager.util;

import java.io.IOException;

/**
 *
 * @author gustavo
 */
public class Memoria {

    private static Double getPercentual(Double total, Double livre, Double maximo) {
        return ((total - livre) * 100) / maximo;
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void getMemory() throws IOException {

        Double memoriaMaxima = (Runtime.getRuntime().maxMemory() / 1024d / 1024d);
        Double memoriaAlocada = ((Runtime.getRuntime().totalMemory()) / 1024d / 1024d);
        Double memoriaUsada = ((Runtime.getRuntime().totalMemory() -
                Runtime.getRuntime().freeMemory()) / 1024d / 1024d);
        Double memoriaLivre = (Runtime.getRuntime().freeMemory() / 1024d / 1024d);

        Double consumo = getPercentual(memoriaAlocada, memoriaLivre, memoriaMaxima);

        System.out.println("maxima: " + memoriaMaxima);
        System.out.println("alocada: " + memoriaAlocada);
        System.out.println("usada: " + memoriaUsada);
        System.out.println("livre: " + memoriaLivre);

        if (consumo > 90) {
            System.out.println("#####################################");
            System.out.println("VAI ESTOURAR");
            System.out.println("#####################################");
            Runtime.getRuntime().gc();
        }
    }
}
